package cn.ultrarisk.scadmin.manager.biz.user.dao;

import cn.ultrarisk.mybatis.extension.domain.Page;
import cn.ultrarisk.mybatis.extension.query.Query;
import cn.ultrarisk.scadmin.manager.biz.solr.dao.SolrCollectionDAO;
import cn.ultrarisk.scadmin.manager.biz.user.entity.Privilege;
import cn.ultrarisk.scadmin.manager.biz.user.entity.Role;
import cn.ultrarisk.scadmin.manager.support.BaseDAO;
import cn.ultrarisk.scadmin.manager.util.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.*;

/**
 * 角色数据操作类
 *
 * @author sevendlong(lin23871@163.com)
 * @date 2014/3/28
 * @since V1.0
 */
@Repository
public class RoleDAO extends BaseDAO<Role, Integer> {
    @Autowired
    private PrivilegeDAO privilegeDAO;
    @Autowired
    private SolrCollectionDAO solrCollectionDAO;

    /**
     * 构造器
     */
    public RoleDAO() {
        super(Role.class, Role.COLUMN_PROPERTY_MAP);
    }

    public Role getByName(String name, Integer idNo) {
        return get(Query.query(Role.NAME, name).neq(Role.ID, idNo));
    }

    public Page<Role> findPage(String name, int pageNo, int pageSize) {
        Query query = Query.query(Role.NAME, name);
        return findPage(query, pageNo, pageSize);
    }

    public void createPrivilegeRelation(Integer roleId, Collection<Integer> privilegeIds) {
        if (roleId != null && CollectionUtil.isNotEmpty(privilegeIds)) {
            List<Privilege> privileges = privilegeDAO.findByIds(privilegeIds);
            Map<Integer, Integer> privilegeIdMap = new HashMap<Integer, Integer>();
            for (Privilege privilege : privileges) {
                privilegeIdMap.put(privilege.getId(), null);
                if (privilege.getParentId() != null) {
                    privilegeIdMap.put(privilege.getParentId(), null);
                }
            }

            Map<String, Object> map = new HashMap<String, Object>();
            map.put("roleId", roleId);
            map.put("privilegeIds", privilegeIdMap.keySet());
            sqlSessionTemplate.insert("Role.createPrivilegeRelation", map);
        }
    }

    public void deletePrivilegeAsso(Integer roleId) {
        sqlSessionTemplate.delete(entityName + ".deletePrivilegeAsso", roleId);
    }

    public Map<Integer, Boolean> findRoleByAdmin(Integer adminId) {
        List<Map<String, Object>> list = sqlSessionTemplate.selectList(entityName + ".listByAdminAsso", adminId);
        Map<Integer, Boolean> result = new HashMap<Integer, Boolean>();
        for (Map<String, Object> map : list) {
            Integer collectionId = Integer.parseInt(map.get("collection_id").toString());
            Integer roleId = Integer.parseInt(map.get("role_id").toString());
            if (collectionId == 0) {
                result.put(roleId, true);
            }
        }
        return result;
    }

    public Map<Integer, Map<Integer, Boolean>> findCollectionRoleByAdmin(Integer adminId) {
        List<Map<String, Object>> list = sqlSessionTemplate.selectList(entityName + ".listByAdminAsso", adminId);
        Map<Integer, Map<Integer, Boolean>> result = new HashMap<Integer, Map<Integer, Boolean>>();
        for (Map<String, Object> map : list) {
            Integer collectionId = Integer.parseInt(map.get("collection_id").toString());
            if (collectionId == 0) {
                continue;
            }
            Integer roleId = Integer.parseInt(map.get("role_id").toString());
            Map<Integer, Boolean> collectionIds = result.get(roleId);
            if (collectionIds == null) {
                collectionIds = new HashMap<Integer, Boolean>();
            }
            collectionIds.put(collectionId, true);
            result.put(roleId, collectionIds);
        }
        return result;
    }

    public void deleteRoleByAdmin(Integer adminId) {
        sqlSessionTemplate.delete(entityName + ".deleteAdminAsso", adminId);
    }

    public void createRoleByAdmin(Integer adminId, Map<Integer, Boolean> roles, Map<Integer, Map<Integer, Boolean>> collectionRoles) {
        List<Map<String, Integer>> list = new ArrayList<Map<String, Integer>>();

        if (roles != null) {
            for (Integer roleId : roles.keySet()) {
                if (roles.get(roleId) != null && roles.get(roleId)) {
                    Map<String, Integer> map = new HashMap<String, Integer>();
                    map.put("admin_id", adminId);
                    map.put("role_id", roleId);
                    map.put("collection_id", 0);
                    list.add(map);
                }
            }
        }
        if (roles != null && collectionRoles != null) {
            for (Integer roleId : collectionRoles.keySet()) {
                if (!roles.containsKey(roleId) || !roles.get(roleId)) {
                    continue;
                }
                if (collectionRoles.get(roleId) != null) {
                    for (Integer collectionId : collectionRoles.get(roleId).keySet()) {
                        if (collectionRoles.get(roleId).get(collectionId) != null && collectionRoles.get(roleId).get(collectionId)) {
                            Map<String, Integer> map = new HashMap<String, Integer>();
                            map.put("admin_id", adminId);
                            map.put("role_id", roleId);
                            map.put("collection_id", collectionId);
                            list.add(map);
                        }
                    }
                }
            }
        }
        sqlSessionTemplate.insert(entityName + ".insertAdminAsso", list);
    }
}
